
package com.demo.hotel_book.controller;

import com.demo.hotel_book.dto.OrderDTO;
import com.demo.hotel_book.entity.Hotel;
import com.demo.hotel_book.entity.HotelOrder;
import com.demo.hotel_book.entity.User;
import com.demo.hotel_book.mapper.HotelMapper;
import com.demo.hotel_book.mapper.HotelOrderMapper;
import com.demo.hotel_book.service.HotelOrderService;
import com.demo.hotel_book.service.OrderZoneService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderZoneController {


    @Autowired
    HotelMapper hotelMapper;
    @Autowired
    HotelOrderMapper hotelOrderMapper;
    @Autowired
    OrderZoneService orderZoneService;
    @Autowired
    private HotelOrderService orderService;

    @RequestMapping("/contact")
    public String goContect(HttpServletRequest request, Model model) {
        HttpSession session= request.getSession();
        User user=(User) session.getAttribute("user");
        List<HotelOrder> byUserId = hotelOrderMapper.findByUserId(user.getId());
      for (int i=0;i<byUserId.size();i++){
          byUserId.get(i).setRoomCode("8088");
          String hotelId = byUserId.get(i).getHotelId();
         // System.out.println(hotelId);
          Hotel hotel = hotelMapper.selectByHotelId(hotelId);
       //   System.out.println(hotel.getHotelTranslatedName());
          byUserId.get(i).setHotelName(hotel.getHotelTranslatedName());

      }

        model.addAttribute("orders",byUserId);

        if (user!=null) {
            PageInfo<OrderDTO> p=orderZoneService.getLogInfoPage(1, 5, user.getId());
//            model.addAttribute("ifPay",orderService.ifPay(orderService.findByUserId(user.getId())));
            //System.out.println(p);
            model.addAttribute("pageinfoOrder",p );
        }
        else
            return "redirect:/signup";
        return "contact";
    }

    @RequestMapping("/queryOrders")
    public String queryKeywords(@RequestParam(defaultValue="1") int pn,
                                               @RequestParam(defaultValue="5") int pageSize,HttpServletRequest request,Model model) {
        HttpSession session= request.getSession();
        User user=(User) session.getAttribute("user");
        if (user==null) {
            return "redirect:/signup";
        }
        model.addAttribute("pageinfoOrder",orderZoneService.getLogInfoPage(pn,pageSize,user.getId()));
        return "contact";
    }

}

